<?php

namespace App\Http\Controllers\System;


use App\Events\CloudEvent;
use App\Model\System\Eeofile;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Model\System\Organization;
use App\Model\System\Token;
use App\Http\Controllers\MessagePacket;

class OrgController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取单一组织的信息
     * @param $id
     * @return packetMessage
     */
    function getOrganization($id)
    {
        $id = 1;
        $org = Organization::find($id);
        if(empty($org)){
            return $this->fail($org, '数据不存在');
        }else{
            return $this->success($org);
        }
    }


    function logo()
    {
        try{
            $id = 1;
            $org = Organization::find($id);
            if(empty($org->agencylogo)){
                throw  new \Exception('not found');
            }else{
                $path = Eeofile::getAbsoluteAddressForApiUrl($org->agencylogo);
                header('Content-type: image/jpeg');
                echo file_get_contents($path);
            }
        }catch (\Exception $e){
            $path = Eeofile::getLocalPath('schoolin.png');
            header('Content-type: image/jpeg');
            echo file_get_contents($path);
        }
    }


    /**
     * 添加一个组织
     * @param Request $request
     * @return packetMessage
     */
    function  addOrganization(Request $request)
    {
        $fields  = $request->all();
        $org = new Organization($fields);
        $org->created_by = Token::user()->userid;
        $org->updated_by = Token::user()->userid;
        if($org->save()){
            $result =$this->dispatchCloudEvent($org);
            if(!$result)
                return $this->success($org);
            else
                return $this->success(false);
            return $this->success($org);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 更新某个组织的账号信息
     * @param Request $request
     * @return packetMessage
     */
    function  updateOrganization(Request $request)
    {
        $id = 1;
        $org = Organization::find($id);
        if(!$org){
            $org = new Organization();
            $org->created_by = Token::user()->userid;
            $org->updated_by = Token::user()->userid;
        }
        foreach ($request->all() as $key=>$val){
            $org->$key = $val;
        }
        $org->updated_by = Token::user()->userid;
        if(!($org->courseminutes > 0))
            return $this->fail(['courseminutes'],'保存失败：课时时长需大于0');
        if(strlen($org->registerDefaultPwd) < 6 || strlen($org->registerDefaultPwd) > 16 )
            return $this->fail(['registerDefaultPwd'],'保存失败：初始密码需要不少于6位，不大于16位');
        if($org->save()){
            $result =$this->dispatchCloudEvent($org);
            if(!$result)
                return $this->success($org);
            else
                return $this->fail(['SID','SafeKey'],'保存失败：EEO对接信息错误：'.$result);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 抛出云盘init事件
     * @param $job
     * @return string
     */
    function dispatchCloudEvent($job)
    {
        try{
            $event = new CloudEvent(CloudEvent::INIT, CloudEvent::FOLDER, 0);
            event($event);
        }catch (\Exception $e){
            return $e->getMessage();
        }

    }


}
